package com.lindroy.androidplayer.logic.network

import android.util.Log.DEBUG
import android.util.Log.VERBOSE
import com.ihsanbal.logging.Level
import com.ihsanbal.logging.Logger
import com.ihsanbal.logging.LoggingInterceptor
import com.lindroy.androidplayer.constants.BASE_URL
import okhttp3.OkHttpClient
import okhttp3.internal.platform.Platform
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * @author Lin
 * @date 2021/4/6
 * @function Retrofit构建器
 */
object ServiceCreator {

    private val client = OkHttpClient.Builder().run {
        addInterceptor(
            LoggingInterceptor.Builder()
                .setLevel(Level.BASIC)
                .log(Platform.INFO)
                .tag("HttpRequest")
//                .addHeader("cityCode", "53")
//                .addQueryParam("moonStatus", "crescent")
                .build()
        )
        build()
    }
    private val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        /* .client(OkHttpClient().newBuilder().addInterceptor { chain ->
             val request = chain.request()
             //获取请求方法和请求路径
 //            Logger.d("${request.method()}：${request.url()}")
             chain.proceed(request)
         }.build())*/
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())//日志拦截器
        .build()

    fun <T> create(serviceClass: Class<T>): T = retrofit.create(serviceClass)

    inline fun <reified T> create(): T = create(T::class.java)
}